<!DOCTYPE html>
<html>
<head>
    <block name="include">
        <include file="Public:header"/>
    </block>
    <style>
        body {
            overflow-y: scroll;
        }
    </style>
</head>
<body class="gray-bg">

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins">
        <div class="ibox-content">
            <div class="layui-tab-content ibox-content">
                <div class="layui-inline">
                    <input type="text" class="layui-input" name="txt" placeholder="搜索关键字" id="search">
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-small" data-type="reload" id="search-btn">搜索</button>
                </div>
                <div>
                    <table class="layui-hide" id="company-table" lay-size="sm" lay-filter="table"></table>
                    <script type="text/html" id="bar">
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<block name="include">
    <include file="Public:footer"/>
</block>
</body>
</html>
<script>
    layui.use('table', function () {
        var table = layui.table;
        table.render({
            elem: '#company-table'
            , url: '{:U("Span/dataGet")}'
//            , cellMinWidth: 500
            , cols: [[
                {field: 'name', title: '公司名'},
                {field: 'boss', width: 80, title: '法人'},
                {field: 'create_date', width: 105, title: '创建时间'},
                {field: 'basement', width: 110, title: '注册资金'},
                {field: 'address', title: '地址'},
                {field: 'span', width: 500, title: '经营范围'},
                {fixed: 'right', width: 150, align: 'center', toolbar: '#bar'}
            ]]
            , page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'],//自定义分页布局
                limits: [50, 100, 200],
                limit: 50
            }
            , id: 'reload'
        });
        var $ = layui.$, active = {
            reload: function () {
                var search = $('#search');
                //执行重载
                table.reload('reload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        search: search.val().trim()
                    }
                });
            }
        };

        $('#search-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.on('tool(table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    layui.use(['jquery', 'layer'], function () {
                        var $ = layui.$
                            , layer = layui.layer;
                        $.ajax({
                            url: "{:U('Span/deltxt')}",
                            data: {id: data.id},
                            dataType: 'json',
                            type: 'post',
                            success: function (data) {
                                if (data.status == 1) {
                                    obj.del();
                                    layer.close(index);
                                }
                            }
                        })
                    })

                });
            }
            if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    title: '修改经营范围信息',
                    area: ['500px', '500px'],
                    maxmin: true,
                    content: ["{:U('Span/edit')}" + '?id=' + data.id]
                });
            }
        });
    });
    //    新增公司名
    layui.use(['jquery', 'layer'], function () {
        var $ = layui.$
            , layer = layui.layer;
        $('#txtAdd').click(function () {
            var value = $('#txt').val();
            if (value.trim() != '' && value != null) {
                $.ajax({
                    url: '{:U("Span/txtAdd")}',
                    data: {'txt': value},
                    dataType: 'json',
                    type: 'post',
                    success: function (data) {
                        if (data.status == 1) {
                            layer.confirm('添加成功', {
                                icon: 1,
                                btn: ['确定']
                            }, function () {
                                $('#txt').val('');
                                window.location.reload();
                            });
                        } else {
                            layer.confirm('添加失败', {
                                icon: 1,
                                btn: ['确定']
                            })
                        }
                    }
                })
            }
        })
    });
</script>
